package DMDE.model.tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import main.java.DMDE.model.Method;
import main.java.DMDE.model.Modifier;
import main.java.DMDE.model.Parameter;
import main.java.DMDE.model.Type;
import main.java.DMDE.model.Visibility;

import org.junit.Before;
import org.junit.Test;

public class MethodTestCase {

	private static final String TEST_METHOD_TYPE = "int";

	private static final String TEST_METHOD_PARAM_NAME = "number";

	private Method method;
	private Parameter parameter;
	private Type type;

	@Before
	public void setUp() {
		method = new Method("method");
		type = new Type(TEST_METHOD_TYPE);
		parameter = new Parameter(new Type(TEST_METHOD_TYPE),
				TEST_METHOD_PARAM_NAME);
	}

	@Test
	public void testCountParameter() {
		method.addParameter(parameter);
		assertEquals(1, method.countParameters());
		Parameter parameter2 = new Parameter(new Type(TEST_METHOD_TYPE),
				"number2");
		method.addParameter(parameter2);
		assertEquals(2, method.countParameters());
	}

	@Test
	public void testRepeatedParameters() {
		Parameter parameter2 = new Parameter(type, TEST_METHOD_PARAM_NAME);
		method.addParameter(parameter);
		method.addParameter(parameter2);
		assertEquals(1, method.countParameters());

	}

	@Test
	public void testGetParameter() {
		method.addParameter(parameter);
		Parameter testParameter = method.getParameter(TEST_METHOD_PARAM_NAME);
		assertNotNull(testParameter);
		assertEquals(parameter, testParameter);
	}

	@Test
	public void testAddParameters() {
		List<Parameter> parameters = new ArrayList<Parameter>();
		for (int i = 0; i < 5; i++) {
			parameters.add(new Parameter(type, String.valueOf(i)));
			method.addParameter(parameters.get(i));
			assertEquals(i + 1, method.countParameters());
		}
		assertEquals(method.countParameters(), 5);
		for (int i = 0; i < 5; i++) {
			assertNotNull(method.getParameter(String.valueOf(i)));
			assertEquals(parameters.get(i),
					method.getParameter(String.valueOf(i)));
		}
	}

	@Test
	public void testReturnType() {
		Type returnType = new Type(TEST_METHOD_TYPE);
		method.setReturnType(returnType);
		assertEquals(returnType, method.getReturnType());
	}

	@Test
	public void testVisibility() {
		method.setVisibility(Visibility.PUBLIC);
		assertEquals(Visibility.PUBLIC, method.getVisibility());
	}

	@Test
	public void testAddModifier() {
		method.addModifier(Modifier.ABSTRACT);
		List<Modifier> modifiers = method.getModifiers();
		assertEquals(Modifier.ABSTRACT, modifiers.get(0));
	}

	@Test
	public void testRepeatedModifier() {
		method.addModifier(Modifier.ABSTRACT);
		method.addModifier(Modifier.ABSTRACT);
		List<Modifier> modifiers = method.getModifiers();
		assertEquals(1, modifiers.size());
	}

}
